package com.xingzhi.kotlindemo;

/**
 * Date:2025/3/24
 * Time:23:39
 * author:wushengqi
 */
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.ViewConfiguration;
import android.widget.HorizontalScrollView;
import android.widget.Scroller;


public class CustomHorizontalScrollView2 extends HorizontalScrollView {
    private static final int MIN_DISTANCE = 200;
    private Scroller mScroller;
    private VelocityTracker mVelocityTracker;
    private int mMinimumVelocity;
    private int mMaximumVelocity;
    private float mLastX;
    private int mInitialScrollX;
    private boolean isAtEnd = false;

    public CustomHorizontalScrollView2(Context context) {
        this(context, null);
    }

    public CustomHorizontalScrollView2(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomHorizontalScrollView2(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mScroller = new Scroller(context);
        ViewConfiguration configuration = ViewConfiguration.get(context);
        mMinimumVelocity = configuration.getScaledMinimumFlingVelocity();
        mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if (mVelocityTracker == null) {
            mVelocityTracker = VelocityTracker.obtain();
        }
        mVelocityTracker.addMovement(ev);

        int action = ev.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                mLastX = ev.getX();
                mInitialScrollX = getScrollX();
                isAtEnd = getScrollX() == getChildAt(0).getWidth() - getWidth();
                if (!mScroller.isFinished()) {
                    mScroller.abortAnimation();
                }
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                mVelocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
                float velocityX = mVelocityTracker.getXVelocity();
                int scrollDistance = Math.abs(getScrollX() - mInitialScrollX);

                if (isAtEnd) {
                    // 已经在最右侧，向左滚动
                    if (velocityX > mMinimumVelocity || scrollDistance > MIN_DISTANCE) {
                        // 滚到最左边
                        mScroller.startScroll(getScrollX(), 0, -getScrollX(), 0);
                    } else {
                        // 回滚到最右侧
                        int targetScrollX = getChildAt(0).getWidth() - getWidth();
                        mScroller.startScroll(getScrollX(), 0, targetScrollX - getScrollX(), 0);
                    }
                } else {
                    // 不在最右侧，向右滚动
                    if (Math.abs(velocityX) > mMinimumVelocity || scrollDistance > MIN_DISTANCE) {
                        // 滚动到底部
                        int targetScrollX = getChildAt(0).getWidth() - getWidth();
                        mScroller.startScroll(getScrollX(), 0, targetScrollX - getScrollX(), 0);
                    } else {
                        // 回滚到最左侧
                        mScroller.startScroll(getScrollX(), 0, -getScrollX(), 0);
                    }
                }
                invalidate();
                if (mVelocityTracker != null) {
                    mVelocityTracker.recycle();
                    mVelocityTracker = null;
                }
                break;
        }
        return super.onTouchEvent(ev);
    }

    @Override
    public void computeScroll() {
        if (mScroller.computeScrollOffset()) {
            scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
            postInvalidate();
        }
    }
}